package com.ics.sapecit.utils.json;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class JSONUtils {
	public static String object2json() {

		return null;
	}

	public static JSONObject string2json() {
		String strJson = "{\"students\":[{\"name\":\"Jack\",\"age\":12}, {\"name\":\"Vista\",\"age\":23}, {\"name\":\"Kaka\",\"age\":22}, {\"name\":\"Hony\",\"age\":31}]}";
		try {
			JSONObject jo = JSONObject.fromObject(strJson);
			JSONArray jsonArray = (JSONArray) jo.get("students");
			for (int i = 0; i < jsonArray.size(); ++i) {
				JSONObject o = (JSONObject) jsonArray.get(i);
				System.out.println("name:" + o.getString("name") + "," + "age:"
						+ o.getInt("age"));
			}
			return jo;
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) {
		string2json();
	}
}
